package com.tang.common.uniform;


import com.tang.common.exception.RecruitExceptionEnum;

/**
 * 封装的统一返回值
 * @param <T>
 */
public class Result<T>{
    //状态码
    private Integer code;
    //状态信息
    private String msg;
    //返回数据
    private T data;
    private static final Integer OK_STATUS=100;
    private static final String OK_MSG="SUCCESS";
    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public Result() {
        this(OK_STATUS,OK_MSG);
    }
    //有数据成功
    public static <T> Result<T> success (T data){
        Result result = new Result();
        result.setData(data);
        return result;
    }
    //无数据成功
    public static <T> Result<T> success (){
        return new Result();
    }
    //数据失败
    public static Result error(Integer code,String msg){
        return new Result(code,msg);
    }

    public static Result error(RecruitExceptionEnum exception){
        return new Result(exception.getCode(),exception.getMsg());
    }



    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
